﻿using System;
using System.Runtime.CompilerServices;

namespace fastCSharp.sql
{
    /// <summary>
    /// JSON成员
    /// </summary>
    /// <typeparam name="valueType"></typeparam>
    [fastCSharp.emit.boxSerialize]
    public struct jsonMember<valueType>
    {
        /// <summary>
        /// 是否值类型
        /// </summary>
        private static readonly bool isValueType = typeof(valueType).IsValueType;
        /// <summary>
        /// 数据对象
        /// </summary>
        public valueType Value;
        /// <summary>
        /// 强制转换定义
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator jsonMember<valueType>(valueType value)
        {
            return new jsonMember<valueType> { Value = value };
        }
        /// <summary>
        /// 强制转换定义
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static implicit operator jsonMember<valueType>(string json)
        {
            if (json.Length == 0) return default(jsonMember<valueType>);
            return new jsonMember<valueType> { Value = fastCSharp.emit.jsonParser.Parse<valueType>(json) };
        }
        /// <summary>
        /// 强制转换定义
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator string(jsonMember<valueType> value)
        {
            return (isValueType || value.Value != null) ? fastCSharp.emit.jsonSerializer.ToJson(value.Value) : string.Empty;
        }

        ///// <summary>
        ///// 对象转换成JSON字符串
        ///// </summary>
        ///// <param name="toJsoner">对象转换成JSON字符串</param>
        ///// <param name="value">参数</param>
        //[fastCSharp.emit.jsonSerialize.custom]
        //private static void toJson(fastCSharp.emit.jsonSerializer toJsoner, jsonMember<valueType> value)
        //{
        //    if (value.Value == null) fastCSharp.web.ajax.WriteNull(toJsoner.CharStream);
        //    else fastCSharp.emit.jsonSerializer.typeToJsoner<valueType>.ToJson(toJsoner, value.Value);
        //}
        ///// <summary>
        ///// 对象转换成JSON字符串
        ///// </summary>
        ///// <param name="parser">Json解析器</param>
        ///// <param name="value">参数</param>
        //[fastCSharp.emit.jsonParse.custom]
        //private static void parseJson(fastCSharp.emit.jsonParser parser, ref jsonMember<valueType> value)
        //{
        //    fastCSharp.emit.jsonParser.typeParser<valueType>.Parse(parser, ref value.Value);
        //}
        /// <summary>
        /// 对象序列化
        /// </summary>
        /// <param name="serializer">对象序列化器</param>
        /// <param name="value"></param>
        [fastCSharp.emit.dataSerialize.custom]
        private static void serialize(fastCSharp.emit.dataSerializer serializer, jsonMember<valueType> value)
        {
            if (value.Value == null) serializer.Stream.Write(fastCSharp.emit.binarySerializer.NullValue);
            else fastCSharp.emit.dataSerializer.typeSerializer<valueType>.Serialize(serializer, value.Value);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="deSerializer">对象反序列化器</param>
        /// <param name="value"></param>
        /// <returns>是否成功</returns>
        [fastCSharp.emit.dataSerialize.custom]
        private unsafe static void deSerialize(fastCSharp.emit.dataDeSerializer deSerializer, ref jsonMember<valueType> value)
        {
            if (deSerializer.CheckNull() == 0) value.Value = default(valueType);
            else fastCSharp.emit.dataDeSerializer.typeDeSerializer<valueType>.DeSerialize(deSerializer, ref value.Value);
        }
    }
}